<?php

namespace App\Http\Controllers\Article;

use App\Article;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        return view('articleCorner')->withArticles(Article::all());
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        // return view('create');
        return view('createQuill');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
        $this->validate($request, [
            'title' => 'required|unique:articles|max:255',
            'body' => 'required',
        ]);
    
        $article = new Article;
        $article->title = $request->get('title');
        $article->body = $request->get('body');
        $article->user_id = 0;
        $article->views = 0;
        $article->votes = 0;
    
        if ($article->save()) {
            return redirect('blog/article');
        } else {
            return redirect()->back()->withInput()->withErrors('保存失败！');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function show(Article $article)
    {
        //
        // $article->body = $this->formatAnswer($article->body);
        return view('article', ['article'=>$article]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function edit(Article $article)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Article $article)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function destroy(Article $article)
    {
        //
    }

    function formatAnswer($answer){
        $formattedAnswer = '';
        $answer = json_decode($answer,true);
        foreach($answer['ops'] as $key=>$element){
            if(empty($element['insert']['image'])){
                $result = $element['insert'];
                if(!empty($element['attributes'])){
                    foreach($element['attributes'] as $key=>$attribute){
                        $result = $this->operate($result,$key,$attribute);
                    }
                }
            }else{ 
                $image = $element['insert']['image'];
                // if you are getting the image as url
                if(strpos($image,'http://') !== false || strpos($image,'https://') !== false){
                    $result = "<img src='".$image."' />";
                }else{
                    //if the image is uploaded 
                    //saving the image somewhere and replacing it with its url
                    $imageUrl = getImageUrl($image);
                    $result = "<img src='".$imageUrl."' />";
                }
            }
            $formattedAnswer = $formattedAnswer.$result;
        }
        return nl2br($formattedAnswer);
    }
    
    function operate($text,$ops,$attribute){
        $operatedText = null;
        switch($ops){
            case 'bold': 
            $operatedText = '<strong>'.$text.'</strong>';
            break;
            case 'italic':
            $operatedText = '<i>'.$text.'</i>';
            break;
            case 'strike':
            $operatedText = '<s>'.$text.'</s>';
            break;
            case 'underline':
            $operatedText = '<u>'.$text.'</u>';
            break;
            case 'link':
            $operatedText = '<a href="'.$attribute.'" target="blank">'.$text.'</a>';
            break;
            default:
            $operatedText = $text;
        }
        return $operatedText;
    }
}
